﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using MyPhotoIndex.Portable.Properties;
using System.Runtime.InteropServices;

namespace MyPhotoIndex.Portable
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.ThrowException, false);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            string basePath = Assembly.GetExecutingAssembly().Location;
            basePath = Path.GetDirectoryName(basePath);
            string prequisitesPath = Path.Combine(basePath, @"Other\PreRequisites");
            string mpiPath = Path.Combine(basePath, @"App\MyPhotoIndex");
            string mpiExe = Path.Combine(mpiPath, @"MyPhotoIndex.exe");
            string dataPath = Path.Combine(basePath, @"Data");
            string photoStorage = Path.Combine(basePath, Settings.Default.DefaultPhotoPath);

            try
            {
                string WIAPath = Path.Combine(prequisitesPath, @"WIA\wiaaut.dll");
                File.Copy(WIAPath, Path.Combine(Environment.SystemDirectory, "wiaaut.dll"), false);
                using (ComRegister register = new ComRegister(WIAPath))
                {
                    register.RegisterComDLL();
                }
            }
            catch { }

            string arguments = string.Format("/adp=\"{0}\" /csl=\"{1}\" /Portable",
                       dataPath,
                       photoStorage);

            Directory.SetCurrentDirectory(mpiPath);

            //       MessageBox.Show(mpiExe);
            ProcessStartInfo startInfo = new ProcessStartInfo(mpiExe, arguments);
            startInfo.UseShellExecute = false;
            startInfo.WindowStyle = ProcessWindowStyle.Maximized;
            startInfo.CreateNoWindow = false;
            Process.Start(startInfo).Dispose();
        }
    }
}
